package com.yycx.module.bus.provider.handler.sbreturn;

import com.yycx.common.constants.CommonConstants;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.OrderNoUtil;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.BusSupplier;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;
import com.yycx.module.bus.provider.service.BusSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 厂家退垫付款保存扩展器
 *
 * @author zyf
 */
@Component("sbRetrunPaymentSaveHandler")
public class SbRetrunPaymentSaveHandler implements SaveInterceptor<BusPayment> {

    @Resource
    private OrderNoUtil orderNoUtil;

    @Autowired
    private BusSupplierService supplierService;


    @Autowired
    private BusOrderDetailsService detailsService;


    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        String selectIds = cs.getParams("selectIds");
        ApiAssert.isNotEmpty("退款明细不能为空", selectIds);
        Long supplierId = payment.getSupplierId();
        ApiAssert.isNotEmpty("supplierId不能为空", supplierId);
        BusSupplier supplier = supplierService.getById(supplierId);
        //应退金额
        BigDecimal supplierBond = payment.getAmount();
        payment.setAmount(supplierBond);
        payment.setAmountType(AmountTypeEnum.SbReturn.getCode());
        payment.setTitle("退垫付款");
        String orderNo = orderNoUtil.getIncrementNum();
        payment.setPaymentNo("TD" + orderNo);
        payment.setPayDate(payment.getAmountDate());
        payment.setOrganizationId(supplier.getOrganizationId());
        payment.setCompanyId(supplier.getCompanyId());
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setPaymentState(CommonConstants.INT_0);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {
        String selectIds = cs.getParams("selectIds");
        detailsService.updateReturnDianfuId(entity,selectIds);
    }


}
